package com.aisino.nmg.ant.task;

import com.aisino.nmg.ant.env.Environment;
import com.aisino.nmg.config.GeneratorProperties;
import com.aisino.nmg.constant.GeneratorConstants;
import com.aisino.nmg.system.generator.model.AppConfig;
import com.aisino.nmg.util.PathUtil;

public class EnvironmentTask extends AbstractTask {
	private Environment environment;
	private GeneratorProperties generatorProperties;

	public EnvironmentTask(GeneratorToolTask parent) {
		super(parent);
	}

	public void setGeneratorProperties(GeneratorProperties generatorProperties){
		this.generatorProperties = generatorProperties;
		Environment environment = new Environment();
		environment.setDbDriver(generatorProperties.getDataSource().getDriverClassName());
		environment.setDbUrl(generatorProperties.getDataSource().getUrl());
		environment.setDbUsername(generatorProperties.getDataSource().getUsername());
		environment.setDbPassword(generatorProperties.getDataSource().getPassword());
		String generatorDir = PathUtil.getRootProjectPath();
		String subWebProjectName = this.getParent().getProject().getArtifactId() + GeneratorConstants.WEB_MODULE;
		String subServiceProjectName = this.getParent().getProject().getArtifactId() + GeneratorConstants.SERVICE_MODULE;
		String subDaoProjectName = this.getParent().getProject().getArtifactId() + GeneratorConstants.DAO_MODULE;
		String subCommonProjectName = this.getParent().getProject().getArtifactId() + GeneratorConstants.COMMON_MODULE;
		environment.setGeneratorDir(generatorDir);
		environment.setSubWebProjectName(subWebProjectName);
		environment.setSubServiceProjectName(subServiceProjectName);
		environment.setSubDaoProjectName(subDaoProjectName);
		environment.setSubCommonProjectName(subCommonProjectName);
		this.environment = environment;
	}

	@Override
	public String getName() {
		return "填充环境参数";
	}

	@Override
	public Integer getSort() {
		return new Integer(0);
	}

	public Environment getEnvironment(){
		return environment;
	}

	@Override
	public void execute() {
		AppConfig appConfig = this.getParent().getAppConfigMapper().selectByMavenInfo(this.getParent().getProject().getArtifactId(), this.getParent().getProject().getGroupId());
		if(appConfig == null){
			throw new RuntimeException("项目不可进行生成代码操作, 检查配置的maven坐标信息是否正确");
		}
		this.environment.setAppconfig(appConfig);
	}
}
